package net.lainiao.morethread.Chapter2.demo3;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/28.
 */
public class EventStore {
    private int maxSize;
    private LinkedList<Date> storage;
    public EventStore(){
        maxSize=10;
        storage=new LinkedList<>();
    }

    public synchronized void set(){
        while (storage.size()==maxSize){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        storage.offer(new Date());
        System.out.println("Set : "+storage.size());
        notifyAll();
    }

    public synchronized void get(){
        while (storage.size()==0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Get : "+storage.size());
        System.out.println("Get Date:"+storage.poll());
        notifyAll();
    }


}
